import { createSlice } from '@reduxjs/toolkit'

export const payNmame = createSlice({
    name: 'payNmame',
    initialState: {
        PayList: [
            {
                id: 1,
                code: '410928200604102154',
                state: false,
                name: '张三（学生）',
                sj: '18237587969'
            },
            {
                id: 2,
                code: '410928200604102222',
                state: false,
                name: '里斯（儿童）',
                sj: '18237587969'
            },
            {
                id: 3,
                code: '410928200604103333',
                state: false,
                name: '王五',
                sj: '18237587969'
            },
            {
                id: 4,
                code: '410928200604104444',
                state: false,
                name: '非凡',
                sj: '18237587969'
            },
        ]
    },
    reducers: {
        getXuan(state, { payload }) {
            const item = state.PayList.find((item) => item.id === payload.id)
            item.state = !item.state
        },
        getDel(state, { payload }) {
            state.PayList = state.PayList.filter((item) => item.id !== payload.id)
        },
        getAdd(state, { payload }) {
            const item = state.PayList.find((item) => item.id === payload.id)
            if (!item) {
                state.PayList.push(payload)
            }
        },
        getdit(state, { payload }) {
            const item = state.PayList.find((item) => item.id === payload.id)
            if (item) {
                Object.assign(item, payload)
            }
        },
    },
})

// Action creators are generated for each case reducer function
export const { getXuan, getDel, getAdd, getdit } = payNmame.actions

export default payNmame.reducer